package com.jms.mybatis.plus.creator;

import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.jms.framework.beans.BeanDefinition;
import com.jms.framework.beans.annotation.Component;
import com.jms.framework.beans.creator.BeanDefinitionCreator;
import com.jms.framework.beans.exception.BeansException;
import com.jms.framework.beans.utils.BeanInitializeUtils;
import com.jms.framework.context.ApplicationContext;
import com.jms.mybatis.plus.beans.MapperBeanDefinition;
import com.jms.tools.util.AnnotationUtils;


import java.lang.annotation.Annotation;

import static com.jms.framework.beans.utils.BeanInitializeUtils.getBeanName;
@Component
public class MapperBeanDefinitionCreator extends BeanDefinitionCreator {

    public MapperBeanDefinitionCreator() {
        super(100);
    }

    @Override
    public BeanDefinition create(Class<?> clazz, ApplicationContext applicationContext) {
        if (Mapper.class.isAssignableFrom(clazz)) {
            try {
                if (BeanInitializeUtils.isBean(clazz)) {
                    Annotation annotation = AnnotationUtils.getAnnotationByTargetAnnotation(clazz, Component.class);
                    if (annotation == null) {
                        return null;
                    }
                    String value = AnnotationUtils.getAnnotationProperty(annotation, "value");
                    return new MapperBeanDefinition(getBeanName(clazz, value), clazz);
                }
            } catch (Exception e) {
                throw new BeansException(e);
            }
        }
        return null;
    }
}
